﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using System.CJL.API.Application.Command.RawMaterialCommand;
using System.CJL.API.DTO;
using System.CJL.Domain;
using System.CJL.Domain.DTO;
using static Share.ParseAPI;

namespace System.CJL.API.Controllers
{
    /// <summary>
    /// 原料控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RawMaterialsController : ControllerBase
    {
        IMediator _mediator;
        public RawMaterialsController(IMediator mediator)
        {
            _mediator=mediator;
        }
        /// <summary>
        /// 原料显示
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<RawMaterial>>> RawMaterialShow([FromQuery]RawMaterialShowCommand t)
        {
            ApiResult<PageDTO<RawMaterial>> result = new ApiResult<PageDTO<RawMaterial>>();
            try
            {
                result.Data= await _mediator.Send(t);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 原料添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> RawMaterialAdd(RawMaterialAddCommand t)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(t);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 原料修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> RawMaterialUpdate(RawMaterialUpdateCommand t)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(t);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 原料详情
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<RawMaterial>> RawMaterialMondel([FromQuery]RawMaterialModelCommand t)
        {
            ApiResult<RawMaterial> result = new ApiResult<RawMaterial>();
            try
            {
                result.Data = await _mediator.Send(t);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
		/// <summary>
		/// 原料删除
		/// </summary>
		/// <param name="t"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<ApiResult<int>> RawMaterialDelete(RawMaterialDeleteCommand t)
		{
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(t);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
		/// <summary>
		/// 原料批量删除
		/// </summary>
		/// <param name="t"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<ApiResult<int>> RawMaterialDeletePi(RawMaterialDeletePiCommand t)
		{
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(t);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
		/// <summary>
		/// 图片上传
		/// </summary>
		/// <param name="file"></param>
		/// <returns></returns>
		/// 
		[HttpPost]
		public  Task<ApiResult<string>>  Uimg(IFormFile file)
		{
            ApiResult<string> result = new ApiResult<string>();
            try
            {
                var SaveFileName = Directory.GetCurrentDirectory() + @"/wwwroot/pic/" + file.FileName;
                using (FileStream fs = new FileStream(SaveFileName, FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                result.Data = file.FileName;
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message= ex.Message;
                throw;
            }
            return Task.FromResult(result);
		}



        /// <summary>
        /// 更新原料库存数量
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateInventory([FromQuery]UpdateInventoryCommand t)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(t);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

    }
}
